<?php
/**
 * Created by Benjamin.
 * Date: 2020/4/30
 * Time: 13:55
 */

namespace App\HttpController;


use App\HttpController\AbstractInterface\REST;
use App\Model\SmsModel;
use App\Model\User\UserModel;
use EasySwoole\Http\Message\Status;
use EasySwoole\Http\Request;
use EasySwoole\Validate\Validate;
class User extends REST
{
    /**
     * 查询用户
     * @Method(allow={GET})
     */
    function GETInfo()
    {
        $param = $this->request()->getRequestParam();
        $model = new UserModel();
        $rs = $model->get($param['userId']);
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], 'fail');
        }
    }

    /**
     * 注册用户
     *  @Method(allow={POST})
     *
     */
    function POSTInfo(){
        $param =  $this->request()->getRequestParam();
        $valitor = new Validate();
        $valitor->addColumn('mobile', '手机号不为空')->required('手机号不为空')->numeric('手机号只能为数字')->lengthMax(11,'手机号最长11位');
        $valitor->addColumn('prefix','区号')->required('区号不能为空')->lengthMin(1,'区号长度错误');
        $valitor->addColumn('code','短信验证码')->required('短信验证码不能为空')->numeric('验证码只能为数字');

        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(Status::CODE_BAD_REQUEST, '', $valitor->getError()->__toString());
        }

        //验证短信验证码
        $smsModel = new SmsModel();
        $sms = $smsModel->order('create_time','DESC')->get(['mobile'=> $param['mobile'],'prefix'=> $param['prefix'],'type'=>1]);
        if(empty($sms)||$sms['content']!=$param['code']){
            return $this->writeJson(Status::CODE_BAD_REQUEST, '', '短信验证码错误');
        }

        $model = new UserModel($param);
        if( $model->getOneByMobile('user_id')){
            return $this->writeJson(Status::CODE_BAD_REQUEST, '', '该手机号已经注册过');
        }

        $model->create_time = time();
        $model->update_time = time();
        $rs = $model->save();
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], $model->lastQueryResult()->getLastError());
        }
    }

    /**
     * 修改用户
     * @Method(allow={PUT})
     */
    function  PUTInfo(){
        $param = $this->request()->getRequestParam();
    }

    /**
     * 登录
     */
    function  POSTLogin(){
        $param = $this->request()->getRequestParam();
        $valitor = new Validate();
        $valitor->addColumn('mobile', '手机号不为空')->required('手机号不为空')->numeric('手机号只能为数字')->lengthMax(11,'手机号最长11位');
        $valitor->addColumn('prefix','区号')->required('区号不能为空')->lengthMin(1,'区号长度错误');
        $valitor->addColumn('code','短信验证码')->required('短信验证码不能为空')->numeric('验证码只能为数字');

        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(Status::CODE_BAD_REQUEST, '', $valitor->getError()->__toString());
        }

        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], $model->lastQueryResult()->getLastError());
        }
    }
}